package com.hlcl.huaji.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.elcl.adapter.FragmentTabAdapter;
import com.elcl.network.AppContext;
import com.elcl.util.StringUtils;
import com.elcl.util.viewutils.WindowUtils;
import com.elcl.widget.comp.toast.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hlcl.huaji.R;
import com.hlcl.huaji.model.Confing;
import com.hlcl.huaji.model.ConfingResult;
import com.hlcl.huaji.server.OrderServer;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lichunlin on 2018/2/28.
 * 主页面
 */

public class HomeActivity extends FragmentActivity {

    public Confing confing;
    private Gson gson = new Gson();
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.ac_home);

        getConfing();
        initFragmentView();
        WindowUtils windowUtils = new WindowUtils();
        windowUtils.fullScreen(HomeActivity.this);
    }

    //获取confing
    private void getConfing() {
        String str_confing = AppContext.getConfing();
        if (StringUtils.isNotBlank(str_confing)){
            ConfingResult confingResult = gson.fromJson(str_confing,new TypeToken<ConfingResult>(){}.getType());
            if (null != confingResult){
                confing = confingResult.getjData();
            }
        }else{
            getConfingServer();
        }
    }

    Handler netHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AppContext.setConfing((String)msg.obj);
            getConfing();
        }
    };

    //获取配置的server
    private void getConfingServer() {
        OrderServer.getInstance().getConfingServer(netHandler);
    }

    private List<Fragment> fragments = new ArrayList<>();
    private RadioGroup rgs;
    private HomeFragment homeFragment = new HomeFragment();
    private MineFragment mineFragment = new MineFragment();
    public FragmentTabAdapter tabAdapter;
    private RadioButton tab_rb_d;

    private void initFragmentView() {
        fragments.add(homeFragment);//首页
        fragments.add(mineFragment);//个人中心

        rgs = (RadioGroup) findViewById(R.id.tabs_rg);
        tabAdapter = new FragmentTabAdapter(this, fragments, R.id.tab_content,
                rgs);
        tabAdapter.setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter.OnRgsExtraCheckedChangedListener() {
            @Override
            public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

            }
        });
    }


    @Override
    public void onBackPressed() {
        exit();
    }

    private long exitTime = 0;

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtils.showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

        }
    }


}
